package com.lzhan.day16;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;

public class DemoURL2 {

	public static void main(String[] args) {
		InputStream input=null;
		BufferedReader bf=null;
		try {
			File file=new File("c:\\","aa.png");
			OutputStream out=new FileOutputStream(file);
			URL url=new URL("https://www.baidu.com//img//bd_logo1.png");
			HttpURLConnection connection=(HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(5*1000);
			connection.setRequestMethod("GET");
			input=connection.getInputStream();
			
			byte[] b=new byte[1024];
			int len;
			while((len=input.read(b))!=-1){
				out.write(b, 0, len);
			}
			System.out.println("网络已经连通");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(bf!=null){
				try {
					bf.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

}
